package org.neuedu.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.neuedu.entity.Department;
import org.neuedu.mapper.DepartmentMapper;
import org.neuedu.service.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 科室表 服务实现类
 * </p>
 *
 * @author lzw
 * @since 2024-11-06
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Override
    public IPage<Department> list(Integer pageNo, Integer pageSize, String name) {
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        wrapper.orderByDesc("id");
        return this.page(new Page<>(pageNo, pageSize), wrapper);
    }

    @Override
    @CacheEvict(value = "admin",key = "'dept'")
    public boolean add(String code, String name, String type) {
        return this.save(new Department(code, name, type));
    }

    @Override
    @CacheEvict(value = "admin",key = "'dept'")
    public Boolean del(Long id,Boolean delmark) {
        return this.updateById(new Department(id,delmark));
    }

    @Override
    public Boolean check(String field,String value,Long id) {
        QueryWrapper<Department> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(field,value);
        if (id !=null){
            queryWrapper.ne("id",id);
        }
        return this.count(queryWrapper)==0;
    }


    @Override
    @CacheEvict(value = "admin",key = "'dept'")
    public Boolean update(Long id,String code, String name, String type) {
        return this.updateById(new Department(id,code,name,type));
    }

    @Override
    @Cacheable(value = "admin",key = "'dept'")
    public List<Department> getActive() {
        QueryWrapper<Department> wrapper = new QueryWrapper<>();
        wrapper.eq("delmark", 1);
        return this.list(wrapper);
    }

    @Override
    public List<Department> getRegisterDept() {
        QueryWrapper<Department> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("type","门诊")
                .eq("delmark",1);
        return this.list(queryWrapper);
    }

}
